package com.atguigu.member.members.controller;

import java.util.Arrays;
import java.util.Map;

import com.atguigu.common.exception.enumtion.ExceptionEnum;
import com.atguigu.member.members.to.AutoLoginResponse;
import com.atguigu.member.members.to.UserLoginVo;
import com.atguigu.member.members.to.UserRegistVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.member.members.entity.UmsMemberEntity;
import com.atguigu.member.members.service.UmsMemberService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;



/**
 * 会员
 *
 * @author qhw
 * @email qhw@gmail.com
 * @date 2021-08-03 20:05:02
 */
@RestController
@RequestMapping("member/member")
public class UmsMemberController {
    @Autowired
    private UmsMemberService umsMemberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("members:umsmember:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = umsMemberService.queryPage(params);

        return R.ok().put("page", page);
    }






    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("members:umsmember:info")
    public R info(@PathVariable("id") Long id){
		UmsMemberEntity umsMember = umsMemberService.getById(id);

        return R.ok().put("umsMember", umsMember);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("members:umsmember:save")
    public R save(@RequestBody UmsMemberEntity umsMember){
		umsMemberService.save(umsMember);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("members:umsmember:update")
    public R update(@RequestBody UmsMemberEntity umsMember){
		umsMemberService.updateById(umsMember);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("members:umsmember:delete")
    public R delete(@RequestBody Long[] ids){
		umsMemberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }




    /**
     * 注册接口
     */
    @PostMapping("/ret")
    public R ret(@RequestBody UserRegistVo userRegistVo){
        umsMemberService.ret(userRegistVo);

        return R.ok();
    }



    /**
     * 登录接口
     */
    @PostMapping("/login")
    public R login(@RequestBody UserLoginVo userLoginVo){
        UmsMemberEntity umsMemberEntity = umsMemberService.login(userLoginVo);
        if(umsMemberEntity!=null){
            return R.ok().setData(umsMemberEntity);
        }

        return R.error(ExceptionEnum.USERNAME_PWD_ERROR.getCode(),ExceptionEnum.USERNAME_PWD_ERROR.getMsg());
    }


    /**
     * 社交登录注册登录接口
     */
    @PostMapping("/auth2.0/login")
    public R autoLogin(@RequestBody AutoLoginResponse autoLoginResponse){
        umsMemberService.autoLogin(autoLoginResponse);
        return R.ok();
    }

}
